package com.itheima.controller;

import com.itheima.mapper.CategoryMapper;
import com.itheima.pojo.*;
import com.itheima.service.AddressService;
import com.itheima.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;

@Controller
public class InfoController {
    @Autowired
    private CustomerService customerService;
    @Autowired
    private AddressService addressService;

    @GetMapping("/info")
    public String info(){
        return "infoCenter";
    }

    @GetMapping("/customer/myInfo")
    public String centerInfo(){
        return "centerInfo";
    }

    @GetMapping("/customer/pass")
    public String pass(){
        return "passIsCorrect";
    }
    //   地址管理页面


    /**
     * 功能：改密码时判断原始密码是否正确
     * @param pass
     * @param request
     * @return
     */
    @ResponseBody
    @PostMapping("/customer/passIsCorrect")
    public int passIsCorrect(String pass, HttpServletRequest request) {
        Customer c = (Customer) request.getSession().getAttribute("customer");
        if (c.getPass().equals(pass)) {
            return 1;
        } else {
            return 0;
        }
    }

    @ResponseBody
    @PostMapping("/customer/pswIsCorrect")
    public int pswIsCorrect(String pass) {
        return customerService.pswIsCorrect(pass);
    }

    /**
     * 功能：修改会员个人信息
     * 实现：用会员的对象进行绑定，注意页面的标签名称和类的属性名一一对应，且有getter and setter
     *
     * @param customer
     * @return
     */
    @PostMapping("/customer/doUpdateMyinfo")
    public String doUpdateMyinfo(HttpServletRequest request, Customer customer) {
        customerService.update(customer);
        // 重写session
        request.getSession().setAttribute("customer", customer);
        return "redirect:/customer/myInfo";
    }   //    这里要重新写session

    @PostMapping("/customer/doUpdatePass")
    public String doUpdatePass(@ModelAttribute("newpass")String newpass, HttpServletRequest request) {
        Customer oldCustomer = (Customer) request.getSession().getAttribute("customer");//获取属性
        // 重写session
        oldCustomer.setPass(newpass);
        customerService.update(oldCustomer);
        return "redirect:/customer/pass";
    }   //    这里要重新写session

    //   地址管理页面

    @GetMapping("/customer/address")
    public String addressManage(Model model, HttpServletRequest request) {
        HashMap<String, Object> prop = new HashMap<>();
        //从session中获取登录用户信息
        Customer customer = (Customer) request.getSession().getAttribute("customer");
        if (customer != null) {
            prop.put("id", customer.getId());
            //在地址表中根据用户ID查找该用户所有的地址，封装后输出到页面
            List<Address> as = addressService.findByProp(prop);
            model.addAttribute("as", as);
        }
        //封装省份信息
        HashMap<String, Object> prop1 = new HashMap<>();
        List<Province> ps = addressService.findAllProv(prop1);
        model.addAttribute("ps", ps);
        //去地址管理页面
        return "address";
    }

    /**
     * 功能：通过省ID获取所有的市
     * @param prov_id
     * @return
     */
    @ResponseBody
    @GetMapping("/customer/getCities")
    public List<City> getCities(@RequestParam("prov_id") int prov_id) {
        HashMap<String, Object> prop = new HashMap<>();
        prop.put("prov_id", prov_id);
        List<City> cities = addressService.findCities(prop);
        return cities;
    }

    /**
     * 功能：通过市ID获取所有的区
     * @param city_id
     * @return
     */
    @ResponseBody
    @GetMapping("/customer/getRegions")
    public List<Region> getRegions(@RequestParam("city_id") int city_id) {
        HashMap<String, Object> prop = new HashMap<>();
        prop.put("city_id", city_id);
        List<Region> regions = addressService.findRegions(prop);
        return regions;
    }

    /**
     * 功能：通过区ID获取所有的乡镇
     * @param region_id
     * @return
     */
    @ResponseBody
    @GetMapping("/customer/getTowns")
    public List<Town> getTowns(@RequestParam("region_id") int region_id) {
        HashMap<String, Object> prop = new HashMap<>();
        prop.put("region_id", region_id);
        List<Town> towns = addressService.findTowns(prop);
        return towns;
    }

    /**
     * 功能：通过乡镇ID获取所有的街道或社区
     * @param town_id
     * @return
     */
    @ResponseBody
    @GetMapping("/customer/getStreets")
    public List<Street> getStreets(@RequestParam("town_id") int town_id) {
        HashMap<String, Object> prop = new HashMap<>();
        prop.put("town_id", town_id);
        List<Street> streets = addressService.findStreets(prop);
        return streets;
    }

    @PostMapping("/doInsert")
    public String doInsert(Address address,HttpServletRequest request) {
        //调用服务层的注册功能
        Customer customer=(Customer) request.getSession().getAttribute("customer");
        address.setCustomer_id(customer.getId());
        addressService.insert(address);
        //重定向至商城首页
        return "redirect:/customer/address";

    }
}

